<?php
/**
 * the base action
 * 
 * @author tom.wang<tom.wang.sz@qq.com>
 * every method may have _before_$method and _after_$method
 */
abstract class AbsQingAction extends AbsQingBase {
	protected $view;
	private $actionName;
	/**
	 * construct
	 */
	public function __construct() {
		if(method_exists($this, '_init')) $this->_init();
		# set view
		$viewClz = ucfirst(strtolower($this->getViewType())) . 'View';
		$this->view = new $viewClz();
	}
	
	/**
	 * set | get | patch set property
	 */
	public function property($property = null, $value = null) {
		$ret = null;
		
		if(is_array($property)) {
			foreach($property as $key => $val) {
				if(property_exists($this, $key)) $this->$key = $val;
			}
		} elseif(is_string($property) && property_exists($this, $property)) {
			if(null === $value) $ret = $this->$property;
			else $this->$property = $value;
		}
		
		return $ret;
	}
	
	/**
	 * get action name
	 */
	public function getName() {
		if(null === $this->actionName) $this->actionName = get_class($this);
		return $this->actionName;
	}
	
	/**
	 * assign a variable
	 */
	public function assign($name, $value) {
		$this->view->assign($name, $value);
	}
	
	/**
	 * fetch a(ll) variable
	 */
	public function fetch($name = null) {
		return $this->view->fetch($name);
	}
	
	/**
	 * display the tpl
	 */
	public function display() {
		$this->view->display();
	}
	
	/**
	 * get the view class
	 *
	 * you can extends this method and decide what view you want
	 */
	protected function getViewType() {
		$ret = null === AC('app_default_view') ? 'qing' : AC('app_default_view');
		
		return $ret;
	}
}